package service;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.game.Card;
import model.game.Game;

/**
 *
 * @author Dustin
 */
public class GameServlet extends HttpServlet {

    private GameController controller;

    @Override
    public void init() {
        controller = new GameController(new Game("Test"));
        controller.addPlayer("foo0", 0);
        controller.addPlayer("foo1", 1);
        controller.addPlayer("foo2", 2);
        controller.addPlayer("foo3", 3);
        controller.getGame().getPlayer(0).setDealer(true);
        controller.getGame().getPlayer(controller.getGame().getDealerIndex()).deal(controller.getGame().getTable(), controller.getGame().getPlayers());

        
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Servlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Servlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String actionCommand = request.getParameter("actie");
        int action = -1;
        int handIndex = -1;
        int tableIndex = -1;

        if(actionCommand.equals("Wissel")) {
            handIndex = Integer.parseInt(request.getParameter("selectOnder"));
            tableIndex = Integer.parseInt(request.getParameter("selectMidden"));
            action = GameController.SWAP;
        } else if(actionCommand.equals("Pas")) {
            action = GameController.PASS;
        } else if(actionCommand.equals("Wissel alles")) {
            action = GameController.SWAP_ALL;
        }

        controller.doTurn(action, handIndex, tableIndex);

        Card[] cards = controller.getTable().getCards();
        for(int i = 0; i < cards.length; i++) {
            request.setAttribute(("TC"+i), cards[i]);
        }

        cards = controller.getGame().getPlayer(0).getHand();
        for(int i = 0; i < cards.length; i++) {
            request.setAttribute(("HC"+i), cards[i]);
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher("/game.jsp");
        dispatcher.forward(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
